package com.clss.webrtclibrary;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.webrtc.EglBase;
import org.webrtc.RendererCommon;
import org.webrtc.SurfaceViewRenderer;

/**
 * @author liuv
 * 应急视频时所有视频画面的显示窗口
 */
@SuppressLint("ViewConstructor")
public class ItemSurfaceView extends RelativeLayout {
    private static final String TAG = "ItemSurfaceView";

    private SurfaceViewRenderer mSurfaceViewRenderer;
    private TextView mNameTv;
    private CheckBox mVoiceSwitchCb;
    private View mView;
    private String mName;
    private OnItemSurfaceViewVoiceSwitchListener mListener;

    public ItemSurfaceView(Context context, EglBase eglBase,String name,
                           String id, OnItemSurfaceViewVoiceSwitchListener listener) {
        super(context);
        this.setTag(id);
        this.mListener = listener;
        this.mName = name;
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        mView = layoutInflater.inflate(R.layout.item_surface_view_layout, null);

        mSurfaceViewRenderer = mView.findViewById(R.id.surface_view);
        mSurfaceViewRenderer.init(eglBase.getEglBaseContext(), null);
        mSurfaceViewRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL);
        mSurfaceViewRenderer.setEnableHardwareScaler(true);
        mSurfaceViewRenderer.setOnClickListener((view) -> {
            mListener.itemSurfaceViewOnClick(getNameId());
        });

        mSurfaceViewRenderer.setOnLongClickListener((view) -> {
            mListener.itemSurfaceViewOnLongClick(getNameId(),mName);
            return true;
        });

        mNameTv = mView.findViewById(R.id.surface_name_tv);
        mVoiceSwitchCb = mView.findViewById(R.id.surface_voice_switch_cb);
        mNameTv.setOnClickListener((view) -> mVoiceSwitchCb.performClick());
        if (!TextUtils.isEmpty(mName)){
            mNameTv.setText(mName);
        }

        mVoiceSwitchCb.setOnClickListener(v -> {
            if (mListener != null){
                mListener.itemSurfaceViewIsChanged(mVoiceSwitchCb.isChecked(), getNameId());
            }
        });

        if (TextUtils.isEmpty(getNameId())){
            mNameTv.setVisibility(GONE);
            mVoiceSwitchCb.setVisibility(GONE);
        }else {
            mNameTv.setSelected(true);
        }
        addView(mView);
    }

    public SurfaceViewRenderer getSurfaceViewRenderer() {
        return mSurfaceViewRenderer;
    }

    public TextView getNameTv() {
        return mNameTv;
    }

    public CheckBox getVoiceSwitchCb() {
        return mVoiceSwitchCb;
    }

    public String getNameId() {
        return getTag() == null ? "" : (String) getTag();
    }

    public void modifyViewSize(int h, int w){
        Lg.i(TAG, "---modifyViewSize==h=" + h);
        Lg.i(TAG, "---modifyViewSize==w=" + w);

        RelativeLayout.LayoutParams layoutParams = (LayoutParams) mView.getLayoutParams();
        layoutParams.height = h;
        layoutParams.width = w;
        mView.setLayoutParams(layoutParams);
    }
}
